var letterCombinations = function(digits) {
  var letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
  var numbers = [2, 3, 4, 5, 6, 7, 8, 9];
  if (digits === "") {
    return
  }
  var splitLetters = [];
  digits.split("").forEach(ele => {
    var intval = parseInt(ele)
    var index = numbers.indexOf(intval)
    var start = index * 3
    if (intval === 7) {
      splitLetters.push(letters.slice(start, start + 4))
    } else if (intval === 8) {
      splitLetters.push(letters.slice(start + 1, start + 4))
    } else if (intval === 9) {
      splitLetters.push(letters.slice(-4))
    } else {
      splitLetters.push(letters.slice(start, start + 3))
    }
  })
  var result = []
  combineLetters(result, splitLetters, digits.length);
  return result
}

function combineLetters(result, spliteLetters, length) {
  var origin = spliteLetters.slice(-length)
  var leftArr = origin[0]
  var rightArr = origin.slice(1, length).flat()
  var curResult = []
  leftArr.forEach(le => {
    rightArr.forEach(re => {
      curResult.push(le + re + "")
    })
  })
  result.push(curResult)
  length--
  if (length === 1) {
    return
  } else {
    combineLetters(result.flat(), spliteLetters, length)
  }
}

letterCombinations("23")